package com.codedancer.campus.manager.api.category;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.category.ro.CategoryAddOrUpdateRo;
import com.codedancer.campus.manager.api.category.ro.CategoryPageSearchRo;
import com.codedancer.campus.manager.service.ICategoryService;
import com.codedancer.campus.manager.service.bo.category.CategoryAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.category.CategoryPageSearchBo;
import com.codedancer.campus.manager.service.vo.category.CategoryPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/category")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "分类Api", description = "分类Api", tags = "分类Api")
public class CategoryApi extends BaseApi {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分页查询分类")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<CategoryPageVo>> page(@ApiParam(value = "分类分页查询Ro", required = false)
        CategoryPageSearchRo categoryPageSearchRo) {
        CategoryPageSearchBo categoryPageSearchBo = BeanUtil.copyProperties(categoryPageSearchRo,
            CategoryPageSearchBo.class);
        return success(categoryService.page(categoryPageSearchBo));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除分类")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "分类uuid", required = true) String uuid) {
        categoryService.delete(uuid);
        return success();
    }

    @PostMapping("/add")
    @ApiOperation("添加分类")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(@RequestBody @Valid @ApiParam(value = "分类添加Ro", required = true)
        CategoryAddOrUpdateRo categoryAddOrUpdateRo) {
        CategoryAddOrUpdateBo categoryAddOrUpdateBo = BeanUtil.copyProperties(categoryAddOrUpdateRo,
            CategoryAddOrUpdateBo.class);
        categoryService.add(categoryAddOrUpdateBo);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改分类")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "分类uuid", required = true) String uuid,
        @RequestBody @Valid @ApiParam(value = "分类修改Ro", required = true) CategoryAddOrUpdateRo categoryAddOrUpdateRo) {
        CategoryAddOrUpdateBo categoryAddOrUpdateBo = BeanUtil.copyProperties(categoryAddOrUpdateRo,
            CategoryAddOrUpdateBo.class);
        categoryAddOrUpdateBo.setUuid(uuid);
        categoryService.update(categoryAddOrUpdateBo);
        return success();
    }
}
